package xyz.sicau.blog.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import xyz.sicau.blog.bean.User;


public interface UserMapper {
    // 登录，验证用户名密码
    @Select("select count(*) from userlogin where user_name=#{userName} and user_pswd=SHA(#{userPswd})")
    int countUserByNameAndPswd(String userName, String userPswd);
    // 向userlogin插入一个用户和加密的密码（注册）
    int insertUserLogin(User user);
    @Insert("insert into user (user_id, user_name) values (#{userId},#{userName})")
    int insertUser(User user);

    //通过用户名查id
    @Select("select user_id from user where user_name = #{userName} limit 1")
    int getUserIdByName(String userName);
    //通过id查用户名
    @Select("select user_name from user where user_id = #{userId}")
    String getUserNameById(int userId);

    // 查询用户，只返回一个
    @Select("select * from user where user_name=#{userName}")
    User getUserByName(String userName);
    @Select("select * from user where user_id=#{userId}")
    User getUserById(int userId);

    // 查询一个用户，通过用户名，返回用户数量，怕后期出现同名的情况
    @Select("SELECT count(*) from user where user_name=#{userName}")
    int getUsersByName(String userName);


    // 获取用户信息====================================
    // 设置
    @Select("SELECT user_setting from user where user_id=#{userId} limit 1")
    String getUserSetting(int userId);


    // 修改用户信息--------------------------------
    // 用户名
    @Update("UPDATE user SET user_name = #{userName} WHERE user_id = #{userId}")
    int updateUserName(int userId, String userName);
    // 头像
    @Update("UPDATE user SET user_img = #{userImg} WHERE user_id = #{userId}")
    int updateUserImg(int userId, String userImg);
    // 信息
    @Update("UPDATE user SET user_info = #{userInfo} WHERE user_id = #{userId}")
    int updateUserInfo(int userId, String userInfo);
    // 设置
    @Update("UPDATE user SET user_setting = #{userSetting} WHERE user_id = #{userId}")
    int updateUserSetting(int userId, String userSetting);

    /**
     * QQ登录接口======================================================
     */
    // 查询openid对应的用户数量
    @Select("SELECT * from user where user_openid_q=#{openidQ} limit 1")
    User getUserByOpenidQ(String openidQ);

    @Update("update user set user_openid_q = #{openidQ} where user_id = #{userId}")
    int bindOpenidById(int userId, String openidQ);
}
